package com.lk.security;

import com.lk.web.entity.SysUser;
import com.lk.web.service.SysPermissionService;
import com.lk.web.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @description: 通过手机号获取用户信息和权限资源
 * @author: Aspirin
 * @create: 2020-05-31 10:44
 */
@Service("mobileUserDetailService")
public class MobileUserDetailServiceImpl extends AbstractUserDetailsService {
  private static final Logger logger = LoggerFactory.getLogger(MobileUserDetailServiceImpl.class);
  @Autowired SysUserService sysUserService;
  @Autowired SysPermissionService sysPermissionService;

  @Override
  public SysUser findSysUser(String usernameOrMobile) {
    logger.info("请求的手机号是:{}", usernameOrMobile);
    // 1. 通过手机号查询用户信息
    SysUser sysUser = sysUserService.findByMobile(usernameOrMobile);
    if (sysUser == null) {
      throw new UsernameNotFoundException("该手机号未注册");
    }
    return sysUser;
  }
}
